package org.sn.jdish.interview.today;

import java.util.HashSet;
import java.util.Set;

public class Test {

	public static void main(String[] args) {
		System.out.println("isLeapYear: " + isLeapYear(2018));
		System.out.println("bits: " + bits(121));
		System.out.println("string2Integer: " + string2Integer("12"));
		System.out.println(removeComments("	/**\r\n" + "	 * 是否闰年\r\n" + "	 * \r\n" + "	 * @param year\r\n"
				+ "	 * @return\r\n" + "	 */\r\n" + "	public static boolean isLeapYear(int year) {\r\n"
				+ "		System.out.println(year);// 打印年份\r\n"
				+ "		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);\r\n" + "	}"));
		int[] array = { 1, 1, 2, 3, 1, 2, 3, 4, 5, 7, 9, 1, 2, 3 };
		System.out.println(unique(array));
	}

	/**
	 * 是否闰年
	 * 
	 * @param year
	 * @return
	 */
	public static boolean isLeapYear(int year) {
		System.out.println(year);
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}

	/**
	 * 返回二进制位中1的个数
	 * 
	 * @param i
	 * @return
	 */
	public static int bits(int i) {
		System.out.println(Integer.toBinaryString(i));
		int count = 0;
		while (i > 0) {
			if ((i & 1) == 1) {
				count++;
			}
			i >>= 1;
		}
		return count;
	}

	/**
	 * 不用java内置函数，将String字符串转换为数字
	 * 
	 * @param str
	 * @return
	 */
	public static int string2Integer(String str) {
		System.out.println(Integer.parseInt(str));
		int resultInt = 0;
		boolean isNegative = false;
		for (char c : str.toCharArray()) {
			if (c == '-') {
				isNegative = true;
			} else {
				resultInt *= 10;
				resultInt += c - '0';
			}
		}
		return isNegative ? -resultInt : resultInt;
	}

	/**
	 * 两个有序数组的合并排序,Java代码实现,并去重复，考虑空间利用率问题
	 * 
	 * @param arr1
	 * @param arr2
	 * @return
	 */
	public int[] mergeSorted(int[] arr1, int[] arr2) {
		int[] result = new int[arr1.length + arr2.length];

		return result;
	}

	/**
	 * 数组去重
	 * 
	 * @param array
	 * @return
	 */
	public static int unique(int[] array) {
		Set<Integer> set = new HashSet<Integer>();
		for (int i : array) {
			set.add(i);
		}
		return set.toArray().length;
	}

	/**
	 * 删除Java代码中的注释
	 * 
	 * @param sourceCode
	 * @return
	 */
	public static String removeComments(String sourceCode) {
		return sourceCode.replaceAll("\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*+\\/", "");
	}

}
